package com.inews.business.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.inews.base.mapper.BeanMapper;
import com.inews.base.persistence.DynamicSpecifications;
import com.inews.base.persistence.SearchFilter;
import com.inews.base.persistence.SearchFilter.Operator;
import com.inews.base.utils.ExtDataUtil;
import com.inews.base.utils.InewsUtils;
import com.inews.base.vo.ExtData;
import com.inews.business.entity.TblRetireStaffHis;
import com.inews.business.entity.TblTeacherHis;
import com.inews.business.repository.TeacherHisDao;
import com.inews.business.vo.RetireStaffHisVo;
import com.inews.business.vo.RetireStaffVo;
import com.inews.business.vo.TeacherHisVo;
import com.inews.business.vo.TeacherVo;

@Service
@Transactional(readOnly=true)
public class TeacherHisServiceImpl implements TeacherHisService{
	
	@Autowired
	private TeacherHisDao teacherHisDao;
	@Autowired
	private ITeacherService teacherService;
	
	
	
	/**
	 * 分页查询人员历史信息.
	 *
	 * @param filterParams the filter params
	 * @param page the page
	 * @return the ext data
	 */
	public ExtData<TeacherHisVo> findTeacherHisPage(Map<String, Object> filterParams, Pageable page, TeacherVo vo){
		List<TeacherHisVo> voList = new ArrayList<TeacherHisVo>();
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
//		filters.put("staffId", new SearchFilter("staffId", Operator.EQ, vo.getStaffId()));
		Page<TblTeacherHis> pagePoList = teacherHisDao.findAll(DynamicSpecifications.bySearchFilter(filters.values(), TblTeacherHis.class), page);
		for (TblTeacherHis po : pagePoList.getContent()) {
			TeacherHisVo hisVo = BeanMapper.map(po, TeacherHisVo.class);
			hisVo.setBatchDateFormat(InewsUtils.fomatDate(po.getBatchDate()));
			voList.add(hisVo);
		}
		ExtData<TeacherHisVo> extData = ExtDataUtil.genWithPagingList(voList, pagePoList.getTotalElements());
		return extData;
	}
	
	
	
	/**
	 * 导出excel.
	 *
	 * @param filterParams the filter params
	 * @param page the page
	 * @return the ext data
	 */
	public ExtData<TeacherHisVo> exportExcel(Map<String, Object> filterParams){
		List<TeacherHisVo> allHisVoList = new ArrayList<TeacherHisVo>();
		ExtData<TeacherVo> data = teacherService.findTeacherPage(filterParams, null);
		List<TeacherVo> voList = data.getRoot();
		for (TeacherVo vo : voList) {
			ExtData<TeacherHisVo> hisData = findTeacherHisPage(new HashMap<String, Object>(), null, vo);
			List<TeacherHisVo> hisVoList = hisData.getRoot();
			for (TeacherHisVo hisVo : hisVoList) {
				allHisVoList.add(hisVo);
			}
		}
		return ExtDataUtil.genWithData(allHisVoList);
	}

}
